package top.yzhao.trackdatasystem.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.yzhao.trackdatasystem.VO.ResultVO;
import top.yzhao.trackdatasystem.dto.UserDTO;
import top.yzhao.trackdatasystem.model.Log;
import top.yzhao.trackdatasystem.model.User;
import top.yzhao.trackdatasystem.service.LogService;
import top.yzhao.trackdatasystem.service.LoginService;

/**
 * User: zhaoyang
 * Date: 2020/3/12
 * Time: 20:58
 * Description: No Description
 */
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private LogService logService;

    /**
     * 登录功能实现
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResultVO<UserDTO> Login(@RequestBody User user) {
        ResultVO<UserDTO> res = new ResultVO<>();
        //System.out.println(user);
        UserDTO data = loginService.Login(user);

        if(data!=null){
            res.setData(data);
            res.setMsg("登录成功！");
            res.setCode(200);
            //增加日志
            Log log = new Log();
            log.setUsername(data.getUsername());
            log.setOperate("/login");
            logService.addLog(log);
        }else {
            res.setMsg("用户名或者密码错误");
            res.setCode(100);
        }
        return res;
    }
}
